function startWebSocket(onOpen, onMessage, onClose, send) {
	var wsuri = "ws://" + HOST.host + "/im-web/websocket";
	var ws = null;
	if ('WebSocket' in window)
		ws = new WebSocket(wsuri);
	else if ('MozWebSocket' in window)
		ws = new MozWebSocket(wsuri);
	else {
		console.log("not support");
		return ws;
	}

	ws.onopen = function(evt) {
		var msg = createMessage(evt);
		console.log("open", msg, evt);
		onOpen(msg);
	};

	ws.onmessage = function(evt) {
		var msg = createMessage(evt);
		console.log("message", msg);
		onMessage(msg);
	};
	
	ws.onclose = function(evt) {
		var msg = createMessage(evt);
		console.log("close", msg);
		onClose(msg);
	};
	
	ws.sendMessage = send;
	
	return ws;
}
//从通信返回内容中，获取消息内容对象
function createMessage(evt) {
	if (evt) {
		var data = evt.data;
		if (data && data.length > 0) {
			data = data.trim().replace('\n','');
			var msg = eval('[' + data + ']')[0];
			msg.messageContent = decodeURIComponent(msg.messageContent);
			return msg;
		}
	}
	return null;
}

function ON_OPEN(msg) {
	
}
//当前用户标识 
var CURRENT_SESSION_ID = null;
var CURRENT_TO_SESSION_ID = null;
var CURRENT_CHAT_TYPE = null;
var USER_LIST_ITEM_ID = 'userListItem_';
function getUserListItemId(sessionId) {
	return USER_LIST_ITEM_ID + sessionId;
}
function ON_MESSAGE(msg) {
	if(msg) {
		switch(msg.senderType - 0) {
			case SenderType.SYSTEM : //系统消息
				if(msg.messageType == MessageType.SYSTEM_ECHO) {	//系统给自身的反馈信息
					CURRENT_SESSION_ID = msg.sessionId;
					console.log("I am login ", CURRENT_SESSION_ID);
					setCurrentUser({id : CURRENT_SESSION_ID});
					var userList = msg.otherSessionIds;
					if(userList) {
						for(var i = 0, len = userList.length; i < len; i ++) {
							addUserToList({id : userList[i]});
						}
					}
				} else if(msg.messageType == MessageType.SYSTEM_BROADCAST) {	//系统发送给触发消息用户之外的其他用户的广播消息
					if(msg.actionType == ActionType.NEW_USER) {	//新用户加入
						console.log("user login ", msg.sessionId);
						addUserToList({id : msg.sessionId});
						OPEN_MESSAGE_BOX_WITH_IF_EXISTS({
							toId : msg.sessionId,
							chatType : msg.chatType,
							ws : webSocket,
							msg : msg,
							isCreateIfNotExist : false,
							isShow : false
						});
					} else if(msg.actionType == ActionType.USER_LEAVE) {
						console.log("user logout ", msg.sessionId);	//用户离开
						deleteUserFromList({id : msg.sessionId});
						OPEN_MESSAGE_BOX_WITH_IF_EXISTS({
							toId : msg.sessionId,
							chatType : msg.chatType,
							ws : webSocket,
							msg : msg,
							isCreateIfNotExist : false,
							isShow : false
						});
					}
				}
				break;
			case SenderType.NORMAL_USER : //用户发送的消息；包含自己发送给自己的消息，因为需要处理信息模板，所以得经过服务器
				OPEN_MESSAGE_BOX_WITH_IF_EXISTS({
					toId : CURRENT_SESSION_ID == msg.senderId ? msg.recipientId : msg.senderId,
					chatType : msg.chatType,
					ws : webSocket,
					msg : msg,
					isCreateIfNotExist : true,
					isShow : false
				});
				break;
			default : ;
		}
	}
}
function SEND_MESSAGE(recipientId, chatType, messageType, msg) {
	var msgObj = {};
	msgObj.senderId = CURRENT_SESSION_ID;
	msgObj.senderType = SenderType.NORMAL_USER;
	msgObj.recipientId = recipientId;
	msgObj.chatType = chatType;
	msgObj.messageType = messageType;
	msgObj.actionType = ActionType.SEND_MSG;
	msgObj.messageContent = encodeURIComponent(msg);
	
	this.send(objToString(msgObj));
	msgObj.messageContent = msg;
	return msgObj;
}
function ON_CLOSE(msg) {
	
}